<?php
/**
 * WAVESELF FRAMEWORK
 * 
 * Waveself framework is an open source project released under New BSD license.
 * Waveself Engine Team did this in order to help you as ourselves with different
 * but still easy to use and learn approach. 
 * 
 * Please read license before you do any changes or to say, before using current software
 * read our licence which can be found under waveself framework root folder.
 * 
 * If you did not recieve copy of licence, contact us immediately and we will send you copy.
 * 
 * @category   Waveself
 * @package    System Configuration
 * @author     Nevio Vesic <nevio.vesic@gmail.com>
 * @copyright  Waveself Framework NPO 2009-2010
 * @license    https://code.google.com/p/waveself-framework/source/browse/trunk/LICENSE   New-BSD
 * @version    0.1a
 */

/**
 * ...
 * 
 * @category    Waveself
 * @package     Settings
 * @author      Nevio Vesic <nevio.vesic@gmail.com>
 * @copyright   Waveself Framework NPO 2009-2010
 * @version     1.0b
 */
class Waveself_Settings extends Waveself_Engine_Abstract
{
	
	/**
	 * Private settings object container
	 *
	 * @var NULL|stdClass
	 */
	private $_s = NULL;
	
	/**
	 * Load configuration file into stage and prepare for manipulation
	 *
	 * @param   String              $file
	 * @return  stdClass
	 * @throws  Waveself_Exception  If file or settings missmatched/not-exist
	 */
	public function __construct($file, $project=null)
	{
		$a = explode('/', $file); $b = count($a);
		$fname = str_ireplace('.php', '', $a[$b-1]);

		/** Let's go check and load file... */
		if(! @fopen($file, 'r', TRUE)) {
			throw new Waveself_Exception("Waveself '{$fname}' settings file inside of '{$project}' project not found or cannot be readed!");
		}
		require_once $file;
		
		/** Let's validate existing settings and import into stage */
		if ( !isset($settings) || !$settings instanceof stdClass) {
			throw new Waveself_Exception("Settings file '{$fname}' inside of '{$project}' project is bugged. Please read docs how to setup settings file!");
		}
		$this->_s = $settings;
		return $this;
	}
	
	/**
	 * Get Settings Container Records
	 *
	 * @return ???|stdClass
	 */
	public function getRecords()
	{
		return $this->_s;
	}
	
	/**
	 * Convert settings object to array!
	 *
	 * @return Array
	 */
	public function toArray()
	{
		return (array)$this->_s;
	}
}